﻿//Force setting jQuery support cross domain.
//This line mostly has effective in IE
jQuery.support.cors = true;
$(document).ready(function () {
    $(document).keyup(function (event) {// press Enter key to Submit form
        if (event.keyCode == 13) {
            $("#btn_login").click();
        }
    });
    function checkLogin() {
        var user = $('#txtUsername_login').val();
        var pass = $('#txtPassword_login').val();
        if (user == '' || user == 'Username') {
            if (pass == '' || pass == 'password') {
                $('#title_pass').css('color', '#ff5050');
            }
            $('#warning').html('Please complete the required fields');
            $('#title_user').css('color', '#ff5050');
            $('#txtUsername_login').val('');
            $('#txtUsername_login').focus();
            $('#warning').css('display', 'block');
            return false;
        }
        if (pass == '' || pass == 'password') {//
            $('#warning').html('Please complete the required fields');
            $('#title_pass').css('color', '#ff5050');
            $('#txtPassword_login').val('');
            $('#txtPassword_login').focus();
            $('#warning').css('display', 'block');
            return false;
        }

        return true;
    }
    $('#btn_login').click(function () {
        if (checkLogin()) {

            $.ajax({
                url: gb_servicePath + "/authenticateduser/login.json",
                crossDomain: "true",
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                traditional: true,
                data: JSON.stringify(buildLogin()),
                "dataType": 'json',
                success: function (data) {
                    writeCookie("userId", data.ReturnData.AuthenticatedUserID, 1);
                    $.ajax({
                        url: gb_servicePath + '/authenticateduser/ID/' + data.ReturnData.AuthenticatedUserID + '.json',
                        crossDomain: "true",
                        type: "GET",
                        contentType: 'application/json; charset=utf-8',
                        traditional: true,
                        data: "",
                        dataType: 'json',
                        success: function (data) {
                            if (data.StatusCode == 200) {
                                var admin = data.ReturnData;
                                if (location.search == "") {
                                    if (admin.UserRoles[0].RoleType == "2") {
                                        window.location.replace('SuperAdminDash');
                                    }
                                    if (admin.UserRoles[0].RoleType == "1") {
                                        window.location.replace('AdminDash');
                                    }
                                    if (admin.UserRoles[0].RoleType == "3" || admin.UserRoles[0].RoleType == "4") {
                                        window.location.replace('ProfileAdmin');
                                    }
                                }
                                else {
                                    window.location.replace('../' + getURLback('ref'));
                                }
                            }
                        },
                        error: function (jqXHR, textStatus, xerror) {
                            if (isBug) {
                                alert("Error in get Roll for User ->" + xerror);
                            }
                        }
                    });
                    //window.location.replace('AdminDash');
                },
                error: function (jqXHR, textStatus) {
                    $('#title_user').css('color', '#ff5050');
                    $('#title_pass').css('color', '#ff5050');
                    $('#warning').html('User name or password invalid, please try again');
                    $('#warning').css('display', 'block');
                    if (isBug) {
                        alert("Error in login");
                    }
                }

            });
        }
        else {
            return false;
        }
    });
});

function buildLogin() {
    var Login = new Object();
    Login.username = $('#txtUsername_login').val();
    Login.password = $('#txtPassword_login').val();
    return Login;
}